package com.lnsf.bookstore.web.coupon.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.annotation.IgnoreAuth;
import com.lnsf.bookstore.common.annotation.LoginUser;
import com.lnsf.bookstore.common.controller.ApiBaseAction;
import com.lnsf.bookstore.common.dao.BaseDao;
import com.lnsf.bookstore.system.userCoupon.vo.UserCoupon;
import com.lnsf.bookstore.web.address.vo.ApiAddress;
import com.lnsf.bookstore.web.coupon.service.ApiCouponService;
import com.lnsf.bookstore.web.coupon.vo.ApiCoupon;
import com.lnsf.bookstore.web.user.vo.ApiUser;
import com.lnsf.bookstore.web.userCoupon.service.ApiUserCouponService;
import com.lnsf.bookstore.web.userCoupon.vo.ApiUserCoupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/coupon")
public class ApiCouponController extends ApiBaseAction{

    @Autowired
    private ApiCouponService apiCouponService;
    @Autowired
    private ApiUserCouponService apiUserCouponService;

    /**
     * 查看个人所有优惠券列表
     */
    @RequestMapping(value = "/list",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Object queryByUid(@LoginUser ApiUser apiUser) {
        Map<String,Object> params = new HashMap();
        JSONObject object = getJsonRequest();
        if(object!=null) {
            Integer status = object.getInteger("status");
            params.put("status", status);
        }
        params.put("userId",apiUser.getUserId());
        params.put("fields","");
        params.put("link","");
        List<ApiCoupon> apiCouponList = apiCouponService.queryAll(params);
        return toResponsSuccess(apiCouponList);
    }

    @RequestMapping("receive")
    @ResponseBody
    public Object add(@LoginUser ApiUser apiUser) {
        JSONObject jsonObject = getJsonRequest();
        Map map = new HashMap();
        map.put("userId",apiUser.getUserId());
        map.put("couponId",jsonObject.getInteger("id"));
        ApiUserCoupon userCoupon = new ApiUserCoupon();
        userCoupon.setAddTime(new Date());
        userCoupon.setCouponId(jsonObject.getInteger("id"));
        userCoupon.setUserId(apiUser.getUserId());
        userCoupon.setCouponNumber(1);
        userCoupon.setStatus(0);
        userCoupon.setIsReceive(1);
        int i = apiUserCouponService.save(userCoupon);
        if(i > 0){
            return queryAll(apiUser);
        }
        return toResponsFail("领取失败");
    }

    @RequestMapping(value = "/queryList4Index",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Object queryAll(@LoginUser ApiUser apiUser) {
        Map<String,Object> resultObject = new HashMap<String, Object>();
        Map<String,Object> params = new HashMap();
        JSONObject object = getJsonRequest();
        if(object!=null) {
            Integer status = object.getInteger("status");
            params.put("status", status);
        }
        params.put("fields","c.*");
        params.put("link"," ");
        List<ApiCoupon> cuponList = apiCouponService.queryAll(params);

        //params.clear();
        params.put("userId",apiUser.getUserId());
        params.put("fields","");
        params.put("link","");
        List<ApiCoupon> userCouponList = apiCouponService.queryAll(params);

        resultObject.put("couponList",cuponList);
        resultObject.put("userCouponList",userCouponList);

        return toResponsSuccess(resultObject);
    }


}
